#include "../includes.h"

#define WARM_UP 		0x00
#define COASTING		0x01
#define RECOVERING		0x02
#define TIME_LOCK		0x03

#define REFERENCE_FREE	0x00
#define	REFERENCE_GPS	0x01
#define	REFERENCE_E1	0x02
#define REFERENCE_LMU	0x03

#define ERROR_BYTE_NORMAL 0
#define ERROR_BYTE_FUNCFAIL 1

//kang #define LOG_PARAM(x) 	((x<<24)&0XFF000000| ((*MyStatusAddr)<<16)&0X00ff0000 |((*YourStatusAddr)<<8)&0X0000FF00|((*FailStatusAddr)<<8)&0X000000FF)
#define LOG_PARAM(x) 	0     //((x<<24)&0XFF000000| (GetFpgaValue(MyStatusAddr)<<16)&0X00ff0000 |(GetFpgaValue(YourStatusAddr)<<8)&0X0000FF00|(0<<8)&0X000000FF )


CM_	CounterModule;
MM_	MyModule;


char	LOCKEDtoGPS_SPEC_IN;
char	LOCKEDtoMASTER_SPEC_IN, init_trace_end;
int	init_trace_counter;
int	Dual_active_counter;
char	Status_DualRx,PrevStatus_DualRx;
int	DualRxNotReceivedCounter;


/* ----------------------------------------------------------- */
//    Dual Operation
/* ----------------------------------------------------------- */
void ChangeModuleStatus(char);
void ChangeModuleStatus_GPS_AVAIL(char);
char GetCounterModuleStatus_GPS_AVAIL(void);
void ChangeModuleStatus_GPS_AVAIL(char);
char GetCounterModuleStatus(void);
void MakeModuleBlock(void);
void ReadYourModuleSE(void);
void MyMonitorSTATUSout(void);

void dual_a_send(void);
void dual_a_recv(int val);
void dual_b_send(void);
void dual_b_recv(int val);
int dual_test_start;
int dual_test_retry;
int dual_test_last_step;
int dual_test_last_val;

int utod_status_array[2];
int utod_status_idx;
extern int debug_test_run;

void Init_DUAL(void)
{
int retval;
char buf[128];

//	if(IsAModule()){
//		SYSTEM.ID = ModuleA;
//		PROMPT[5] = 'A';
//
//		if(!debug_test_run){
//			retval = system("ifconfig eth0 192.168.10.19 netmask 255.255.255.0 broadcast 192.168.10.255 up");
//		}
//		retval = mknod("/etc/aside",(0666),0);
//		if(retval == -1){
//			sprintf(buf,"Touch-a, %s",strerror(errno));
//			LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, buf);
//		}
//	}
//	else {
//		SYSTEM.ID = ModuleB;
//		PROMPT[5] = 'B';
//
//		if(!debug_test_run){
//			retval = system("ifconfig eth0 192.168.10.20 netmask 255.255.255.0 broadcast 192.168.10.255 up");
//		}
//		retval = mknod("/etc/bside",(0666),0);
//		if(retval == -1){
//			sprintf(buf,"Touch-b, %s",strerror(errno));
//			LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, buf);
//		}
//	}
	
	UnconditionalSigOff();

    MyModule.StatusEvent = NO;
    MyModule.AlarmTimer = 0;

    MyModule.SelfBlock = NO;
    MyModule.ForceBlock = NO;

    MyModule.Command_SelfBlock = NO;
    MyModule.Command_ForceBlock = NO;

	MyModule.sig = OFF;

	CounterModule.STATUS = NOT_EXIST;
	CounterModule.EXIST = NO;
	CounterModule.AliveCounter = 0;

	CounterModule.status = 0;
	CounterModule.error = 0;
	CounterModule.opmode = 0;
	CounterModule.inform = (BIT_A_ANT_DISABLE|BIT_B_ANT_DISABLE);

	CounterModule.RxFlag = OFF;

	CounterModule.delay = 0;

	CounterModule.standby_counter = 0;

	LOCKEDtoGPS_SPEC_IN = FAIL;
	LOCKEDtoMASTER_SPEC_IN = FAIL;
	init_trace_end = NO;
	init_trace_counter = 0;

	Dual_active_counter = 0;

	monitor.dual = OFF;

	Status_DualRx = PrevStatus_DualRx = NO;

	DualRxNotReceivedCounter = 0;
	
	SetDualTxModeToOperation();
	
	dual_test_start = YES;
	dual_test_retry = 0;
}


dual_data_	dual_data;
void SendMyStatus()
{
	int i;
	unsigned char checksum;
	unsigned char *msg;
char s[10];

	int byte = 0;
	
	//dual_data_	dual_data;

	
	dual_data.header = DUAL_DATA_HEADER;
	dual_data.length = DUAL_DATA_LENGTH;;

	//dual_data.model_id = SYSTEM.Model_ID;
	dual_data.model_id = OSC.kind;

	dual_data.year		= (BYTE)(SYSTEM.TIME.UTC.YEAR-2000);
	dual_data.month		= SYSTEM.TIME.UTC.MONTH;
	dual_data.day		= SYSTEM.TIME.UTC.DAY;
	dual_data.hour		= SYSTEM.TIME.UTC.HOUR;
	dual_data.minute	= SYSTEM.TIME.UTC.MINUTE;
	dual_data.second	= SYSTEM.TIME.UTC.SECOND;

	dual_data.status	= TOD.status;
	dual_data.error		= TOD.error;
	dual_data.opmode	= TOD.opmode;
	dual_data.inform	= TOD.inform;


	dual_data.delay		= (unsigned char)CONFIG.delay_value;
	
	dual_data.reserved[0]	= psda.my_priority_value;
	for( i = 1; i < 5; i++){
	//for( i = 0; i < 5; i++){
		dual_data.reserved[i]	= 0;
	}

	if(SYSTEM.ID == ModuleA){dual_a_send();}
	else{dual_b_send();}
	
	msg = (BYTE*)&dual_data;
	checksum = 0x00;

	for(i=0;i<dual_data.length-1;i++){
		checksum ^= msg[i];
	}

	dual_data.checksum	= checksum;

	
	//kang TransmitnMessage((BYTE*)&dual_data,dual_data.length,PORT_DUAL1);
	if(dual_tx_debug){
		DIAG.DUAL_TX = FAIL;
	}
	else{
		//kang ---------------------------------------------------------------------------------
		if( (byte = write(fdDUAL, msg, sizeof(dual_data)) ) == -1 ){
			//printf("Write failed\n");
			if(DIAG.DUAL_TX != FAIL){
				sprintf(MESG,"Dual tx, %s",strerror(errno));
				LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
			}
			DIAG.DUAL_TX = FAIL;
		}	
		else{	
			//fflush(fdDUAL);	
			//printf("SendMyStatus : ");
			//for( i=0; i<sizeof(dual_data); i++){
			//	//printf("%d  ", *(msg+i) );
			//}
	    	//printf("\n\n");
	    	if(DIAG.DUAL_TX != OK){
	    		LogItString(LOG_CATA1_NORM, LOG_CATA2_OK, "Dual tx");
	    	}
	    	DIAG.DUAL_TX = OK;
		}
		//kang---------------------------------------------------------------------------------
	}


	//if(monitor.dual){
    //
	//	sprintf(MESG,"\r\n(Tx) %.3d: ",TimeSliceCounter);
	// 	TransmitMessage((BYTE*)MESG,DEBUG_PORT);
    //
	//	for( i = 0; i < dual_data.length; i++ ){
    //
	//		MESG[0] = hex_char[(msg[i]>>4)&0xf];
	//		MESG[1] = hex_char[(msg[i]   )&0xf];
	//		MESG[2] = ' ';
	//		TransmitnMessage((BYTE*)MESG,3,DEBUG_PORT);
	//	}
    //
	// 	TransmitMessage((BYTE*)"\r\n",DEBUG_PORT);
	//}
	
	if(monitor.dual){
    
		sprintf(MESG,"(Tx) %.3d: ",TimeSliceCounter);
	 	
		for( i = 0; i < dual_data.length; i++ ){
    		sprintf(s,"%c%c ",hex_char[(msg[i]>>4)&0xf], hex_char[(msg[i]   )&0xf]);
    		strcat(MESG,s);
		}
		strcat(MESG,"\n");
    
	 	TransmitMessage((BYTE*)MESG,DEBUG_PORT);
	}


}


void SendIamDead(void)
{
	TOD.status = 0;
	TOD.error = 0;
	TOD.opmode = 0;
	TOD.inform = 0;
}


void ReadYourReserved1(BYTE *buffer,int len)
{
int j;	
unsigned char checksum;
dual_data_	*pdual_data;

	if(len!=20){return;}
	
	pdual_data = (dual_data_*)&buffer[0];
	
	if (monitor.dual){
		sprintf(MESG,
			"(Rx) %.3d: %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X"
			" %.2X %.2X %.2X %.2X %.2X"
			" %.2X\n",
			TimeSliceCounter,
			pdual_data->header,
			pdual_data->length,
			pdual_data->model_id,
			pdual_data->year,
			pdual_data->month,
			pdual_data->day,
			pdual_data->hour,
			pdual_data->minute,
			pdual_data->second,
			pdual_data->status,
			pdual_data->error,
			pdual_data->opmode,
			pdual_data->inform,
			pdual_data->delay,
			pdual_data->reserved[0],
			pdual_data->reserved[1],
			pdual_data->reserved[2],
			pdual_data->reserved[3],
			pdual_data->reserved[4],
			pdual_data->checksum
		);

		TransmitMessage((unsigned char*)MESG,PORT_CONTROL);

	}
		
	for( j = 0, checksum = 0; j < (DUAL_DATA_LENGTH-1); j++ ){
		checksum ^= buffer[j];
	}

	if( pdual_data->header == DUAL_DATA_HEADER && pdual_data->checksum == checksum){
		if(SYSTEM.ID == ModuleA)	{dual_a_recv(pdual_data->reserved[1]);}
		else						{dual_b_recv(pdual_data->reserved[1]);}
	}
}


void ReadYourStatus(BYTE *buffer,int *Counter)
{

	int i,j,index;
	unsigned char checksum,mask;
	dual_data_	*pdual_data;


	index = *Counter;

	if(CounterModule.EXIST == NO){
		*Counter = 0;
		utod_status_array[utod_status_idx] = -20;
		return;
	}

	utod_status_array[utod_status_idx] = *Counter;

	DIAG.DualRxFailCounter=0;
	DIAG.DualRxFailHopeless=0;
	if(DIAG.DUAL_RX != OK){
		LogItString(LOG_CATA1_NORM, LOG_CATA2_OK, "Dual rx");
	}
	DIAG.DUAL_RX = OK;

	
	for( i = (DUAL_DATA_LENGTH-1); i < index; i++ ){

		pdual_data = (dual_data_*)&buffer[i-(DUAL_DATA_LENGTH-1)];
				
		for( j = 0, checksum = 0; j < (DUAL_DATA_LENGTH-1); j++ ){
			checksum ^= buffer[i-(DUAL_DATA_LENGTH-1)+j];
		}

		if( pdual_data->header == DUAL_DATA_HEADER && pdual_data->checksum == checksum){

			DIAG.DualRxChkOKCounter++;
			//if(DIAG.DualRxChkOKCounter>10){
			//	DIAG.DualRxChkOKCounter = 10;
			if(DIAG.DualRxChkOKCounter>0){
				DIAG.DualRxChkOKCounter = 10;
				if((DIAG.DUAL_RX_CRC != OK) && (DIAG.DUAL_STEP == TRY_NULL)){
					LogItString(LOG_CATA1_NORM, LOG_CATA2_OK, "Dual rx chksum");
				}
				DIAG.DUAL_RX_CRC = OK;
			}
	
			CounterModule.RxFlag = ON;

			CounterModule.status 	= pdual_data->status;
			CounterModule.error 	= pdual_data->error;
			CounterModule.opmode 	= pdual_data->opmode;
			CounterModule.inform 	= pdual_data->inform;

			CounterModule.delay	= pdual_data->delay;

			psda.your_priority_value	= pdual_data->reserved[0];


			if( SYSTEM.ID == ModuleA ) {
				CounterModule.LockMode = (pdual_data->opmode >> 2) & 0x03;
				CounterModule.ErrMode = (pdual_data->error >> 2) & 0x01;
			}
			else {
				CounterModule.LockMode = (pdual_data->opmode >> 4) & 0x03;
				CounterModule.ErrMode = (pdual_data->error >> 3) & 0x01;
			}

			// Active �̸� ������ ACTIVE
			//if(AreYouActive() && (psda.your_priority_value == dpComplete) && (DIAG.DUAL_TEST==OK) &&
			//	(CounterModule.LockMode != WARM_UP)) 
			//{
			if(AreYouActive() && (psda.your_priority_value == dpComplete) && (DIAG.DUAL_TEST==OK))
			{
//printf(">>> readyour:%d,%d\n",AreYouActive(),psda.your_priority_value);
				CounterModule.STATUS = STATUS_ACTIVE;
			}
			else {
				//if(DIAG.DUAL_TEST!=OK){
				if((DIAG.DUAL_TEST!=OK) || (DIAG.DUAL_STEP!=TRY_NULL)){
					CounterModule.STATUS = STATUS_UNKNOWN;
				}
				else if(AreYouSwitchReady() && (CounterModule.ErrMode != ERROR_BYTE_FUNCFAIL)){
				//if(AreYouSwitchReady() && (DIAG.DUAL_TEST==OK) && (CounterModule.LockMode != WARM_UP)){
					CounterModule.STATUS = STATUS_STANDBY;
				}
				else {
					CounterModule.STATUS = STATUS_BLOCK;
				}
/*
					// fail�� ��� ��� Lock �̻� �Ǿ��� ��쿡�� standby , �׷��� ������ block
					if(CounterModule.LockMode == WARM_UP){
						CounterModule.STATUS = STATUS_BLOCK;
					}
					else {
						CounterModule.STATUS = STATUS_STANDBY;
					}
*/
			}

			if( SYSTEM.ID == ModuleA ) {
				if( pdual_data->inform & 0x40 ){
					CounterModule.osc = Rubidium_OSC;
				}
				else {
					CounterModule.osc = OCXO_OSC;
				}
			}
			else {
				if( pdual_data->inform & 0x80 ){
					CounterModule.osc = Rubidium_OSC;
				}
				else {
					CounterModule.osc = OCXO_OSC;
				}
			}

			if( SYSTEM.ID == ModuleA ) {
				CounterModule.reference = (pdual_data->opmode >> 0) & 0x03;
			}
			else {
				CounterModule.reference = (pdual_data->opmode >> 6) & 0x03;
			}

			//CounterModule.reference = REFERENCE_GPS;

			if((CounterModule.STATUS==STATUS_ACTIVE) && (TOD.turn==OFF) && (SYSTEM.TIME.Valid)){
				//if(TimeSliceCounter >= 20 && TimeSliceCounter <= 40){
					SYSTEM.TIME.UTC.YEAR 	= (WORD)((WORD)pdual_data->year + 2000);
					SYSTEM.TIME.UTC.MONTH 	= pdual_data->month;
					SYSTEM.TIME.UTC.DAY 	= pdual_data->day;
					SYSTEM.TIME.UTC.HOUR 	= pdual_data->hour;
					SYSTEM.TIME.UTC.MINUTE 	= pdual_data->minute;
					SYSTEM.TIME.UTC.SECOND 	= pdual_data->second;
				//}
			}
			*Counter = 0;
			break;
		}
		else{
			DIAG.DualRxChkOKCounter = 0;
			if((DIAG.DUAL_RX_CRC != FAIL) && (DIAG.DUAL_STEP == TRY_NULL)){
				sprintf(MESG,
					"Dual rx chksum, "
					"%.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X"
					" %.2X %.2X %.2X %.2X %.2X"
					" %.2X",
					pdual_data->header,
					pdual_data->length,
					pdual_data->model_id,
					pdual_data->year,
					pdual_data->month,
					pdual_data->day,
					pdual_data->hour,
					pdual_data->minute,
					pdual_data->second,
					pdual_data->status,
					pdual_data->error,
					pdual_data->opmode,
					pdual_data->inform,
					pdual_data->delay,
					pdual_data->reserved[0],
					pdual_data->reserved[1],
					pdual_data->reserved[2],
					pdual_data->reserved[3],
					pdual_data->reserved[4],
					pdual_data->checksum
				);
					
				LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
			}
			DIAG.DUAL_RX_CRC = FAIL;
		}
	}
}



void ReadYourModuleSE(void)
{
/*
	if( CounterModule.EXIST == NO ){	// No Backup module
		CounterModule.status = 0x80;	// Fault
		CounterModule.error = 0x20;	    // Mo module
	}
*/
}



void ReadYourModuleExist(void)
{
	//---------------------------------
	//     Counter Module STATUS
	//---------------------------------

	if ( CounterModule.EXIST == YES ) {

		if ( DIAG.u_exist == NO ){
//			if((CounterModule.EXIST != NO) && (DIAG.DUAL_STEP == TRY_NULL)){
//				sprintf(MESG,"%s not alive, 0x%x", ((SYSTEM.ID==ModuleA)?"B-side":"A-side"), GetEvents());
//				LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, MESG);
//			}
			
			if(DIAG.DUAL_STEP == TRY_NULL){
				CounterModule.EXIST = NO;
				CounterModule.STATUS = NOT_EXIST;
			}
		}
	}
	else {

		if( DIAG.u_exist == YES ) {

			if(CounterModule.AliveCounter < 6 ){
				CounterModule.AliveCounter++;
			}

		} else {

			CounterModule.AliveCounter = 0;
		}

		if( CounterModule.AliveCounter == 6 ){
			//if((CounterModule.EXIST != YES) && (DIAG.DUAL_STEP == TRY_NULL)){
//			if((CounterModule.EXIST != YES)){
//				sprintf(MESG,"%s alive, 0x%x", ((SYSTEM.ID==ModuleA)?"B-side":"A-side"), GetEvents());
//				LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, MESG);
//			}
			CounterModule.EXIST = YES;
		}
//		else {
//			CounterModule.EXIST = NO;
//			CounterModule.STATUS = NOT_EXIST;
//		}
	}
}

int SigOnValue=0;
char ReadMySigStatus(void)
{

	char sig;

	if( IsSigOn() ){ 	sig = ON;		}
	else {				sig = OFF;		}
		
	//kang if( EVENT.PLL == ON || USER.MANUAL_SIG_ON == ON ){
	if( USER.MANUAL_SIG_ON == ON ){
		//printf("(%d %d)",EVENT.PLL , USER.MANUAL_SIG_ON );
		sig = UNDEFINED;
	}

	if( DIAG.i_exist == NO ){
		DIAG.DELETION_COUNTER = 20;
	}else {
		if(DIAG.DELETION_COUNTER > 0) DIAG.DELETION_COUNTER--;
	}

	return sig;
}


void CheckPriority()
{
	if( SYSTEM.ID == ModuleA ) {
		MyModule.LockMode = (TOD.opmode >> 4) & 0x03;
	}
	else {
		MyModule.LockMode = (TOD.opmode >> 2) & 0x03;
	}

	if( SYSTEM.ID == ModuleA ) {
		MyModule.reference = (TOD.opmode >> 6) & 0x03;
	}
	else {
		MyModule.reference = (TOD.opmode >> 0) & 0x03;
	}

	CounterModule.reference = MyModule.reference = REFERENCE_GPS;
	CounterModule.osc = MyModule.osc = OSC.kind;


	//*******************************************************//
	// Reference
	//*******************************************************//
	// 1. Counter Module

	if( CounterModule.LockMode == TIME_LOCK || CounterModule.LockMode == RECOVERING ){

		if( CounterModule.reference == REFERENCE_GPS ){
			if( CounterModule.osc == Rubidium_OSC ){
				CounterModule.priority = 1;
			}
			else {
				CounterModule.priority = 2;
			}
		}
		else if( CounterModule.reference == REFERENCE_LMU ){
			if( CounterModule.osc == Rubidium_OSC ){
				CounterModule.priority = 3;
			}
			else {
				CounterModule.priority = 4;
			}
		}
		else if( CounterModule.reference == REFERENCE_E1 ){
			if( CounterModule.osc == Rubidium_OSC ){
				CounterModule.priority = 5;
			}
			else {
				CounterModule.priority = 6;
			}
		}
		else {
			if( CounterModule.osc == Rubidium_OSC ){
				CounterModule.priority = 7;
			}
			else {
				CounterModule.priority = 8;
			}
		}
	}
	else {
		if( CounterModule.osc == Rubidium_OSC ){
			CounterModule.priority = 7;
		}
		else {
			CounterModule.priority = 8;
		}
	}

	// 2. My Module

	if( MyModule.LockMode == TIME_LOCK || MyModule.LockMode == RECOVERING ){

		if( MyModule.reference == REFERENCE_GPS ){
			if( MyModule.osc == Rubidium_OSC ){
				MyModule.priority = 1;
			}
			else {
				MyModule.priority = 2;
			}
		}
		else if( MyModule.reference == REFERENCE_LMU ){
			if( MyModule.osc == Rubidium_OSC ){
				MyModule.priority = 3;
			}
			else {
				MyModule.priority = 4;
			}
		}
		else if( MyModule.reference == REFERENCE_E1 ){
			if( MyModule.osc == Rubidium_OSC ){
				MyModule.priority = 5;
			}
			else {
				MyModule.priority = 6;
			}
		}
		else {
			if( MyModule.osc == Rubidium_OSC ){
				MyModule.priority = 7;
			}
			else {
				MyModule.priority = 8;
			}
		}
	}
	else {
		if( MyModule.osc == Rubidium_OSC ){
			MyModule.priority = 7;
		}
		else {
			MyModule.priority = 8;
		}
	}

/*
	if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG){

		sprintf(MESG,"{%d %d %d %d}",
									MyModule.priority, MyModule.LockMode,
									CounterModule.priority, CounterModule.LockMode);
		TransmitMessage(MESG,PORT_CONTROL);
	}
*/

}


int ChangeTime = 0;
void SwitchingProcess (char SIG)
{
	
	CheckPriority();

	switch(SIG){

		case ON:

				switch(SYSTEM.OPERATION.STATUS){
					case STATUS_ACTIVE:// �̰� �翬�ϴ�. ��ȭ�� ��ٴ� �߱�.
							// ������� Block -> UnconditionalSigOn();
							// ������� Standby -> SigOnWithReadyOff();			
							switch(CounterModule.STATUS){

								case	NOT_EXIST:
											CounterModule.standby_counter = 0;
											UnconditionalSigOn();
//printf(">>>sp-24\n");
											break;
								case	STATUS_BLOCK:
											CounterModule.standby_counter = 0;
											SigOnWithReadyOff();
											// Active �� ���� ������� �����ų� Block �� �� ���
											// First standby ���·� �������ش�. [2002-04-08 11:57����]
											FIRST_STANDBY = YES;
//printf(">>>sp-23\n");
											break;

								//case	STATUS_PRE_STANDBY:
								//			SigOnWithReadyOff();
								//			break;
								case	STATUS_STANDBY:

											CounterModule.standby_counter++;

											SigOnWithReadyOff();
//printf(">>>sp-22\n");
											//*******************************************************//
											// �켱������ �� ��ü ����.
											//*******************************************************//
											if( MyModule.priority > CounterModule.priority ){

												//if(CounterModule.standby_counter > 120*2){
												//�켱������ ���� Standby���� Active�� ��ü�Ǳ� ���� �ð��� 2�п��� 3������ ����.
												if(CounterModule.standby_counter > 120*3){
													MyModule.Command_SelfBlock = ON;
													MyModule.SelfBlock = ON;
													
													sprintf(MESG,"Self block-1, %d,%d",
														MyModule.priority,
														CounterModule.priority
													);
													LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
													//printf("\r\nCounterModule.standby_counter > 120*3");
//printf(">>>sp-21\n");
												}
											}

											//*******************************************************//
											// Fault �� ���� ��ü����.
											// ���⼭�� ����⿡�� Fail �� ������ Ȯ���ϰ� �Ѱܾ��Ѵ�.
											//*******************************************************//
											// DIAG.SYSTEM = OSC, REFERENCe, POWER
											if(	DIAG.SYSTEM == FAIL ){
												MyModule.Command_SelfBlock = ON;
												MyModule.SelfBlock = ON;
												
												sprintf(MESG,"Self block-2, %d,%d,%d,%d,%d",
													DIAG.POWER, DIAG.OSC, DIAG.DUAL_RX, 
													DIAG.DUAL_TEST, DIAG.TRACE_10M_COUNTER
												);
												LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
												//printf("\r\nDIAG.SYSTEM == FAIL");
											}
											break;
								case	STATUS_UNKNOWN:
											CounterModule.standby_counter = 0;
											break;
								default:
											CounterModule.standby_counter = 0;
											UnconditionalSigOn();
//printf(">>>sp-19\n");
											break;
							}
							break;

					case STATUS_STANDBY:	

							// Your-Self Block�̸�.
							//if(AreYouFail()){
							if(AreYouFailLGE()){
								// (1) U_HW_FAIL�� ����, ������� �̻��� ��� ����̹Ƿ�,
								// �ٷ� Active�� ���� �ȴ�.
								ClearYourFail();
//printf(">>>sp-18\n");
							//MessageOut("<1>");

							}

							//MessageOut("<4>");

								// ������ 1PPS Alarm�� ���� ��ü�� ���
								// recovery2 �� �����Ѵ�. [2000-09-19 1:01����]
							if( DIAG.PPS_RECOVERY_FLAG == ON ){
								DIAG.PPS_RECOVERY_FLAG = OFF;
								//PPS_ALARM_RECOVERY_CONTROL = ON;
								RECOVERY.flag = ON;
								RECOVERY.wait_time = 1;
								RECOVERY.average_time = 20;

							//MessageOut("<5>");

							}

							MyModule.StatusEvent = STANDBY_TO_ACTIVE;
							LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
								LOG_SWITCH_S2A,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-17\n");
							break;

					//===================================================================
					// �� ���� ������ Power�� ������ ��, Fail�� ���� ������ �����ϴ�.
					case STATUS_PRE_STANDBY:
						
							MyModule.StatusEvent =  STANDBY_TO_ACTIVE;
							LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
								LOG_SWITCH_S2A,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-16\n");
							break;

					case STATUS_BLOCK:	

							// �־��� ��� �ϳ��� �?�ε�, �ٸ� �༮�� Power-off�� ����
							// �������� �Ѿ�´�.
							// �� Powerup->Warmup ���� Block��  UnconditionalSigOff �̹Ƿ�
							// �̷� ���� �߻����� �ʴ´�.


							//UnconditionalSigOff();  //���ٸ���..


							MyModule.StatusEvent = BLOCK_TO_ACTIVE_STANDBY;
							MessageOut("<3:B2AS>");
							LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
								LOG_SWITCH_B2AS,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-15\n");
							break;
				}
				break;


		case OFF:

				switch(SYSTEM.OPERATION.STATUS){
					case STATUS_ACTIVE:// �̰�, � ������ ���ؼ� ������� Standby -> Active�� ����
									// ���� Active-> blocked�� �Ǿ�ٴ� �߱�.

							// ��ɿ� ���� ���� Standby�� �ٷ� ������.
							if( MyModule.SelfBlock == ON ) {
								MyModule.SelfBlock = OFF;
								//ClearMyFail();

								if(	DIAG.SYSTEM == OK ){// && 0){
									MyModule.StatusEvent = ACTIVE_TO_STANDBY;
									LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
										LOG_SWITCH_A2S,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-14\n");
								} else {
									MyModule.StatusEvent = ACTIVE_TO_BLOCK;
									LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
										LOG_SWITCH_A2B,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-13\n");
								}
							}
							else {
								if( DIAG.i_exist == NO ) {
									MyModule.StatusEvent = ACTIVE_TO_BLOCK;
									LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
										LOG_SWITCH_A2B,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-12\n");
								}
								else {

									MyModule.StatusEvent = ACTIVE_TO_BLOCK;
									LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
										LOG_SWITCH_A2B,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-11\n");
								}
							}
							break;

					case STATUS_STANDBY:
						
							SigOffWithReadyOn();
//printf(">>>sp-10\n");
							if ( (DIAG.SYSTEM == FAIL) || (DIAG.i_exist == NO) ) {
								MyModule.StatusEvent = STANDBY_TO_BLOCK;
								LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
									LOG_SWITCH_S2B,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-9\n");
							}
							break;
							
					case STATUS_PRE_STANDBY:

							UnconditionalSigOff();
//printf(">>>sp-8\n");
							if(LOCKEDtoMASTER_SPEC_IN == OK /* && FIRST_LOCK*/){
								MyModule.StatusEvent = PRE_STANDBY_TO_STANDBY;
								LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
									LOG_SWITCH_PS2S,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-7\n");
							}


							if (  (DIAG.SYSTEM == FAIL) || (DIAG.i_exist == NO) ) {
								MyModule.StatusEvent = STANDBY_TO_BLOCK;
								LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
									LOG_SWITCH_S2B,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-6\n");
							}

							if( (CounterModule.STATUS != STATUS_ACTIVE) || (CounterModule.STATUS == STATUS_UNKNOWN)){
								MyModule.StatusEvent = PRE_STANDBY_TO_BLOCK;
								LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
									LOG_SWITCH_PS2B,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-5\n");
							}

							break;

					case STATUS_BLOCK:	

							// Active �Ǵ� Standby �� �� �� �̻� ������ ���� Fail ���� ������ Block�� ���
							if(FIRST_LOCK){
//printf(">>>sp-4-1\n");
								// My Module �� �� ������ ���, ��� ����� Active �� ��� �־��� ��츦 ����Ͽ� Ready On
								if ( (CounterModule.STATUS == STATUS_ACTIVE) && (DIAG.i_exist == YES) &&
									 (DIAG.DELETION_COUNTER == 0) && (DIAG._FAULT == OFF) && 
									 (LOCKEDtoMASTER_SPEC_IN==OK) )
								{
									SigOffWithReadyOn();
//printf(">>>sp-4\n");
								}
								else {
									UnconditionalSigOff();
//printf(">>>sp-3\n");
								}

								// My Module �� ������ ��� ��� Standby�� ������.
								if ( (DIAG.SYSTEM == OK) && (DIAG.i_exist == YES) && (DIAG.DELETION_COUNTER == 0)){

									if(MyModule.StatusEvent == NONE){
										MyModule.StatusEvent = BLOCK_TO_ACTIVE_STANDBY;
										MessageOut("<4:B2AS>");
										//2004-06-01 5:59����
										LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
											LOG_SWITCH_B2AS,GetParameter1(),GetParameter2(),GetParameter3());
//printf(">>>sp-2\n");
									}
								}
							}
							// �ʱ� Warmup ����
							else {
//								if(psda.my_priority_value<dpSigOn){
									UnconditionalSigOff();
//								}
//printf(">>>sp-1\n");
							}
							break;
				}
				break;
	}

	if( SYSTEM.OPERATION.STATUS == STATUS_ACTIVE && CounterModule.STATUS == STATUS_ACTIVE ){
		Dual_active_counter++;
	}
	else {
		Dual_active_counter = 0;
	}

	if( Dual_active_counter == 10){
		if( SYSTEM.ID == ModuleB ){
			MyModule.StatusEvent = ACTIVE_TO_BLOCK;
			LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, 999,LOG_PARAM(0));
			//LogIt3(LOG_CATA1_NORM, LOG_CATA2_UNSORTED, LOG_SWITCH_A2B,GetParameter1(),GetParameter2(),GetParameter3());
		}
	}
}


//---------------------------------
//    STATUS event
//---------------------------------
void SwitchingEvent(char SIG)
{
	switch(MyModule.StatusEvent){

		case BLOCK_TO_ACTIVE_STANDBY:

				if(DIAG.SYSTEM == OK){

					switch(CounterModule.STATUS){
						//���� Active�� �� �� �ִ� ��Ȳ
						case STATUS_BLOCK:
						case NOT_EXIST:
    					case STATUS_STANDBY:

                    			//UnconditionalSigOn();
							
								//kang 09-11-19--------------------------------------------------------------------
								if( psda.firstActiveCnt ==0 && psda.your_priority_value == dpSigOn){ 
									psda.firstActiveCnt++; 
									psda.my_priority_value = dpBeforActive;
									SigOffWithReadyOn();

								}
                    			else{  
									//kang 09-11-19--------------------------------------------------------------------
									psda.firstActiveCnt=1;
									psda.my_priority_value = dpComplete;
									UnconditionalSigOn();
									SYSTEM.OPERATION.STATUS = STATUS_ACTIVE;
	     							SYSTEM.OPERATION.CTRLMODE = CTRL_LOCKEDtoGPS;
	
	     							// CONTROL.SYNC = ON;
	                    					// ���� Standby�� õ�̵Ǿ��� �� ������ �̸� ������ �ɷ� ������ �ξ�� �ȴ�.
	                    			LOCKEDtoMASTER_SPEC_IN = OK;
	
	                    			LogIt(LOG_CATA1_NORM, LOG_CATA2_IMPORTANT1, LOG_Active,LOG_PARAM(1));
	
	        						STATUS_Warmup = OFF;

	        					}
                    			break;

						case STATUS_ACTIVE:
    							// IS-2000 ���� ����[2000-03-11 15:42]

		             			// 1. ������� Active�̰�, �������� ��� �ϰ� ������
    	             			//    ���� �ϴ� Pre_standby �� �����ϵ� Control��
        	         			//    Locked To Master���� ������ �Ѵ�.
            	     			//    ���Ŀ� Master Tracking�� �����ϸ�, �� ���� ��μ� Standby���� ����.

	        	     			// 2. ������� Active���� ������ ��Ȳ�̸� ( Error )
    	             			//    ���� �ϴ� Pre_standby �� �����ϵ� Control��
        	         			//    Locked To Master���� ������ �Ѵ�.
            	     			//    ���Ŀ� Master Tracking�� �����ϸ�,
            	     			//    �� ���� ������ ������ Blocked�� ����� ���� Active ���� ����.

     							//b1 = ReadYourModuleSE();

            	     			//if(CounterModule.S_CODE){
            	     				//
            	     			//}

     				        	InitializeTraceControl();

 	   	    					SYSTEM.OPERATION.STATUS = STATUS_PRE_STANDBY;
     							SYSTEM.OPERATION.CTRLMODE = CTRL_LOCKEDtoMASTER;
								psda.my_priority_value = dpComplete;

          			   			//SigOffWithReadyOn();
    	             			break;
						
						case STATUS_UNKNOWN:
								break;
								
    	             	default:
                    			UnconditionalSigOn();
								psda.my_priority_value = dpComplete;

 	            	    		SYSTEM.OPERATION.STATUS = STATUS_ACTIVE;
     							SYSTEM.OPERATION.CTRLMODE = CTRL_LOCKEDtoGPS;

     							// CONTROL.SYNC = ON;
                    			// ���� Standby�� õ�̵Ǿ��� �� ������ �̸� ������ �ɷ� ������ �ξ�� �ȴ�.
                    			LOCKEDtoMASTER_SPEC_IN = OK;

                    			LogIt(LOG_CATA1_NORM, LOG_CATA2_IMPORTANT1, LOG_Active,LOG_PARAM(1));

        						STATUS_Warmup = OFF;
		             			break;
					}
					
					if(CounterModule.STATUS != STATUS_UNKNOWN){
						MyModule.StatusEvent = NONE;
					}

				}
				else{
					if((CounterModule.STATUS == NOT_EXIST) && (SIG == ON)){
						psda.firstActiveCnt=1;
						psda.my_priority_value = dpComplete;

						UnconditionalSigOn();
						SYSTEM.OPERATION.STATUS = STATUS_ACTIVE;
						SYSTEM.OPERATION.CTRLMODE = CTRL_LOCKEDtoGPS;
	
						// CONTROL.SYNC = ON;
       					// ���� Standby�� õ�̵Ǿ��� �� ������ �̸� ������ �ɷ� ������ �ξ�� �ȴ�.
               			LOCKEDtoMASTER_SPEC_IN = OK;

               			LogIt(LOG_CATA1_NORM, LOG_CATA2_IMPORTANT1, LOG_Active,LOG_PARAM(1));
	
   						STATUS_Warmup = OFF;
   						
   						//if(CounterModule.STATUS != STATUS_UNKNOWN){
							MyModule.StatusEvent = NONE;
						//}
					}
				}
				//Clear_DEBUG_Bit();

				break;

		// �̰� �̹� ������ Active�� ����, Locked to Master������ ���������� ���̴�.
		// ��� �� �κ��� Control�ʿ��� ȣ���Ѵ�.
		case PRE_STANDBY_TO_STANDBY:

				// ���⼭ PLL�� �ٽ� �ʱ�ȭ�Ѵ�.
				//EVENT.PLL = ON;


       			SYSTEM.OPERATION.STATUS = STATUS_STANDBY;
       			SigOffWithReadyOn();

				//SetMeSwitchReady();


				MyModule.StatusEvent = NONE;

				// [99-04-12 5:28����]
				// �̰�, �� ������� DAC���� ������ ���ؼ�..
				TRACE.DACTableIndex = 0;

				LogIt(LOG_CATA1_NORM, LOG_CATA2_IMPORTANT1, LOG_Standby,LOG_PARAM(2));

				STATUS_Warmup = OFF;
				//Claer_DEBUG_Bit();
//MessageOut("BLKtoACTSTB-8\n");
				break;



		// ��� ����� STATUS�� ��� ����Ī�� �����Ѵ�.
		case ACTIVE_TO_BLOCK:
				SYSTEM.OPERATION.STATUS = STATUS_BLOCK;
				UnconditionalSigOff();
				
				//2004-06-01 5:08����
				ClearMyFail();
//MessageOut("BLKtoACTSTB-9\n");
//				InitializeKalmanControl();
//    			Init_QuickStart();

				switch(CounterModule.STATUS){
					// �� ��쿡�� �� ����� GPS�� �����Ѵ�.
					case STATUS_ACTIVE:
					case STATUS_STANDBY:
				        	InitializeTraceControl();
							if ( DIAG.i_exist == NO ){
								LOCKEDtoMASTER_SPEC_IN = OK;
//printf(">>>se-10\n");
							}
							SYSTEM.OPERATION.CTRLMODE = CTRL_LOCKEDtoMASTER;
//printf(">>>se-9\n");
							break;
					case STATUS_BLOCK:
							SYSTEM.OPERATION.CTRLMODE = CTRL_LOCKEDtoGPS;
//printf(">>>se-8\n");
							break;
					case STATUS_UNKNOWN:
							SYSTEM.OPERATION.CTRLMODE = CTRL_LOCKEDtoGPS;
							break;
				}


				MyModule.StatusEvent = NONE;

				LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_Block,LOG_PARAM(4));
//printf(">>>se-7\n");
				break;


		// �̰� Ŀ�ǵ忡 ���ؼ� Switching�Ǵ� ����,
		// Active -> Standby�� ���Ѵ�

		case ACTIVE_TO_STANDBY:

       			SYSTEM.OPERATION.STATUS = STATUS_STANDBY;

				CONTROL.RealDAC = ave_dac.recent;
	        	//InitializeTraceControl();
				InitializeTraceControlSecond();

				SYSTEM.OPERATION.CTRLMODE = CTRL_LOCKEDtoMASTER;

				MyModule.StatusEvent = NONE;

				LogIt(LOG_CATA1_NORM, LOG_CATA2_IMPORTANT1, LOG_Standby,LOG_PARAM(8));

       			SigOffWithReadyOn();
				ClearMyFail();
//MessageOut("BLKtoACTSTB-10\n");
				break;


		// �̰� �׳� ������ �ٲٸ� �ȴ�.
		// ������� STATUS�� �ƹ� ����� ���.
		case STANDBY_TO_BLOCK:
				SYSTEM.OPERATION.STATUS = STATUS_BLOCK;

				MyModule.StatusEvent = NONE;

				LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_Block,LOG_PARAM(3));
//printf(">>>se-5\n");
				break;

		// Pre standby ���¿��� ������� Ż��� ���
		// ��� Locked to GPS �� �ٲپ �ش�.
		case PRE_STANDBY_TO_BLOCK:

				SYSTEM.OPERATION.STATUS = STATUS_BLOCK;
				SYSTEM.OPERATION.CTRLMODE = CTRL_LOCKEDtoGPS;
				MyModule.StatusEvent = NONE;
//printf(">>>se-4\n");
				break;

		case STANDBY_TO_ACTIVE:

				if(/*(CounterModule.LockMode != LOCKED) && */(SYSTEM.OPERATION.LOCKMODE == LOCKED)){

					MessageOut("Recovery start!\r\n");
					RECOVERY.flag = ON;
					RECOVERY.IntervalCounter = 0;
					RECOVERY.wait_time = 5;
					RECOVERY.average_time = 30;
				}


				SYSTEM.OPERATION.STATUS = STATUS_ACTIVE;

				// ���⼭ ACTIVE�� �����ϴ� ����� Trace�� ���ؼ� �ѱ��.!!!
        		UnconditionalSigOn();

	        	//InitializeKalmanControl();
	        	/************ ���־��� �߿�.. *********/

	        	//CONTROL.PrevDAC=CONTROL.ControlDAC=CONTROL.RealDAC=CONTROL.NominalDAC=MeanDAC=TRACE.MeanDAC = ave_dac.recent;
	        	CONTROL.PrevDAC=CONTROL.ControlDAC=CONTROL.RealDAC=CONTROL.NominalDAC=MeanDAC=TRACE.MeanDAC;
				PWM(); // to Set Update dac Value; the same as calling ChangeDACValue(int fd, int volt) function

				SYSTEM.OPERATION.CTRLMODE = CTRL_LOCKEDtoGPS;

				MyModule.StatusEvent = NONE;

				LogIt(LOG_CATA1_NORM, LOG_CATA2_IMPORTANT1, LOG_Active,LOG_PARAM(5));

				STATUS_Warmup = OFF;
//printf(">>>se-3\n");
				break;

		default:
				MyModule.StatusEvent = NONE;
//printf(">>>se-2\n");
				break;
	}

	//---------------------------------------------------
	//             Switching Command�� �����ϴ� �κ�.
	//---------------------------------------------------
	// TOD�� ������ �ð��� �����ش�.
//	if( TimeSliceCounter > 50 ){

	    if(MyModule.Command_SelfBlock == ON ){

    		MyModule.Command_SelfBlock = OFF;
   			SetMeFail();
//printf(">>>se-1\n");
	    }
//    }
}

_PseudoActive psda={.my_priority_value=0, .your_priority_value=0, .firstActiveCnt=0, .prev_my_priority_value=0, .prev_your_priority_value=0 };

//function to enable the signal during warmup 
// return value
int ipsdaWaitTimeout=0;
void ProcessPseudoActive(char *sig) 
{
		//if( (CounterModule.STATUS == STATUS_ACTIVE) || (SYSTEM.OPERATION.STATUS == STATUS_ACTIVE ) || psda.my_priority_value == dpComplete || psda.your_priority_value == dpComplete  ){
		if( (CounterModule.STATUS == STATUS_ACTIVE) || 
		//	(SYSTEM.OPERATION.STATUS == STATUS_ACTIVE || SYSTEM.OPERATION.STATUS == STATUS_STANDBY)  )
			(STATUS_Warmup == OFF))
		{
			psda.my_priority_value = dpComplete;
			psda.your_priority_value = dpComplete;
			ipsdaWaitTimeout=0;
			return ;
		}
		else ipsdaWaitTimeout++;
		
		if( ipsdaWaitTimeout < 10 ) return ;
		else ipsdaWaitTimeout = 10;

		*sig =  0;
		//kang 2009-11-19--------------------------------------------------------------------------------------------------------------
		//if( (CounterModule.STATUS != STATUS_ACTIVE) && (SYSTEM.OPERATION.STATUS != STATUS_ACTIVE) ){
		if(1){
		
			psda.firstActiveCnt = 0;
			if( DIAG.SYSTEM == FAIL )								psda.my_priority_value = dpFAULT;
			//else if( psda.my_priority_value == dpSigOn )	;		//psa.my_priority_value = dpSigOn;
			else if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP )	psda.my_priority_value = dpPWR_UP;
			else if( SYSTEM.TIME.Valid )							psda.my_priority_value = dpTOD;
			else if( SYSTEM.OPERATION.OPMODE == OPMODE_WARMUP )		psda.my_priority_value = dpWARMUP;
			//else if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP )	psda.my_priority_value = dpPWR_UP;
			
			if( DIAG.u_exist == NO ) psda.your_priority_value = dpNOT_EXIST;
//printf("pseudo-0:%d,%d\n",psda.my_priority_value,psda.your_priority_value);				
			if( (psda.your_priority_value<dpSigOn) && ( psda.my_priority_value<dpSigOn) && ( psda.my_priority_value==dpTOD) ){
//printf("pseudo-1:%d,%d\n",psda.my_priority_value,psda.your_priority_value);
				psda.my_priority_value = dpSigOn;
			}
			if( psda.my_priority_value==dpSigOn )
			{
				//if( AreYouExist() )	SigOnWithReadyOff();
				//else 				UnconditionalSigOn();
				UnconditionalSigOn();
//printf("pseudo-2:%d,%d\n",psda.my_priority_value,psda.your_priority_value);
			}
            if( psda.firstActiveCnt ==1 ){ 
            	SigOffWithReadyOn(); 
//printf("pseudo-3:%d,%d\n",psda.my_priority_value,psda.your_priority_value);
            }
			
		}
		/*
		if( 		
			(psda.prev_your_priority_value != psda.your_priority_value) ||
			(psda.prev_my_priority_value   != psda.my_priority_value)
		) printf("\r\n m):p:%d, c:%d, u):p:%d, c:%d", psda.prev_my_priority_value, psda.my_priority_value, psda.prev_your_priority_value, psda.your_priority_value );
		*/
		
		if( psda.your_priority_value == dpBeforActive && psda.my_priority_value==dpSigOn)
		{
//printf("pseudo-4:%d,%d\n",psda.my_priority_value,psda.your_priority_value);
			SigOnWithReadyOff();
			SetMeFail();
			ClearMyFail();
			
			psda.my_priority_value=dpComplete;
			SetMeNotActive();
			UnconditionalSigOff();	
		}
		
		//dummy code below
		if( (CounterModule.STATUS == STATUS_ACTIVE) && (psda.my_priority_value==dpSigOn) ){
//printf("pseudo-5:%d,%d\n",psda.my_priority_value,psda.your_priority_value);
			psda.my_priority_value=dpComplete;
			SetMeNotActive();
			UnconditionalSigOff();	
		}
		
		psda.prev_your_priority_value = psda.your_priority_value;
		psda.prev_my_priority_value   = psda.my_priority_value;
		//&& CounterModule.STATUS!=NOT_EXIST ){ 
		*sig = OFF; // pretend sigOnOffValue just like off


}


void MonitorSTATUS()
{
	char sig;
	
	int i = 0;
	int n = 0;
	unsigned char rcvmsg[256] = {0,};
	dual_data_	dual_data1;
	

	if(utod_status_idx==0){utod_status_idx=1;}
	else{utod_status_idx=0;}
    //kang------------------------------------------------
	
	n=read(fdDUAL, rcvmsg, sizeof(rcvmsg));
	
	ReadYourReserved1(rcvmsg,n);
	
	//printf("ReadYourStatus : ");
	if( DIAG.u_exist == YES ){
		//if (monitor.dual){
		//	TransmitMessage((unsigned char*)"[you alive]\n",PORT_CONTROL);
		//}
		
		//n=read(fdDUAL, rcvmsg, sizeof(dual_data1));
		//n=read(fdDUAL, rcvmsg, sizeof(rcvmsg));
		
		//printf("n = %d\n", n);
		//if(n<0){printf("################ dual com. error!\n"); } 
		if((n != 20) || (dual_rx_debug)){
			DIAG.DualRxFailCounter++;
			if(DIAG.DualRxFailCounter>20){
				DIAG.DualRxFailCounter = 0;
				
				DIAG.DualRxFailHopeless++;
				if(DIAG.DualRxFailHopeless<5){
					//set_tty( fdDUAL, B57600, 2 );//sewon
				}
				
				if(DIAG.DUAL_RX != FAIL){
					LogItString1(LOG_CATA1_NORM, LOG_CATA2_FAIL, "Dual rx,",n);
				}
				DIAG.DUAL_RX = FAIL;
			}
		}
		else{
			ReadYourStatus(rcvmsg, &n);
		}

		if((DIAG.DUAL_EXIST != YES) && (DIAG.DUAL_STEP == TRY_NULL)){
			sprintf(MESG,"%s exist, 0x%x", ((SYSTEM.ID==ModuleA)?"B-side":"A-side"), GetEvents());
			LogItString(LOG_CATA1_NORM, LOG_CATA2_PLUS_YOU, MESG);
		}
		DIAG.DUAL_EXIST = YES;
	}
	else{
		utod_status_array[utod_status_idx] = -10;
		if((DIAG.DUAL_EXIST != NO) && (DIAG.DUAL_STEP == TRY_NULL)){
			sprintf(MESG,"%s not exist, 0x%x", ((SYSTEM.ID==ModuleA)?"B-side":"A-side"), GetEvents());
			if(DIAG.DUAL_EXIST == YES){
				LogItString(LOG_CATA1_NORM, LOG_CATA2_MINUS_YOU, MESG);
			}
			else{
				LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, MESG);
			}
		}
		DIAG.DUAL_EXIST = NO;
		
		if(DIAG.DUAL_STEP == TRY_NULL){
			DIAG.DUAL_RX = UNDEFINED;
			DIAG.DUAL_RX_CRC = UNDEFINED;
			dual_test_start = YES;
			dual_test_retry = 0;
			DIAG.DUAL_TEST = UNDEFINED;
			DIAG.TRACE_10M_COUNTER = UNDEFINED;
		}
		
		DIAG.DualRxChkOKCounter = 0;
		DIAG.DualRxFailCounter = 0;
		DIAG.DualRxFailHopeless = 0;
	}

			
	//for( i=0; i<sizeof(dual_data1); i++){
	//	printf("%d  ", rcvmsg[i] );
    //}
	
	//kang----------------------------------------------------	


//	if( SYSTEM.IO.CTRL_PORT.MODE == UDEBUG2){
	if( iLsiDebugVariable == 9927 ){

		sprintf((char*)MESG,"%.6X ",LOG_PARAM(0));
		TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	}

	//---------------------------------
	//     Counter Module ���� �Ǵ�.
	//---------------------------------
	ReadYourModuleExist();
	
	//2004-02-11 11:15����
	//---------------------------------
	//		����ȭ ������ ���� Ȯ��.
	//---------------------------------
	if(	CounterModule.RxFlag == OFF){
		if(DualRxNotReceivedCounter<10){
			DualRxNotReceivedCounter++;
		}
	}
	else{
		DualRxNotReceivedCounter = 0;
		Status_DualRx = YES;
	}

	if(DualRxNotReceivedCounter>=20){//10���̻�..
		Status_DualRx = NO;
	}

	// Serial �� ��޵Ǵ� dual status �� ����� ��޵Ǿ��� ��쿡�� ����ȭ������ �ϵ����Ѵ�.
	//kang if( CounterModule.EXIST == NO || CounterModule.RxFlag == ON) {
	//if(1){	
	//if(DIAG.DUAL_STEP==TRY_NULL){
	if(DIAG.DUAL_STEP==TRY_NULL){

		CounterModule.RxFlag = OFF;
		//---------------------------------
		//     Counter Module STATUS �б�
		//---------------------------------
		ReadYourModuleSE();

		//---------------------------------
		//    ���� STATUS �б�
		//---------------------------------
		// ���� Signal On/Off ���¸� ����.
		// Deleting ���¸� ����.
		sig = ReadMySigStatus();
		
		ProcessPseudoActive(&sig);

		if( MyModule.sig == OFF && sig == ON ){	
			MyModule.sig = ON;	
			LogIt(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_SIG_ON,LOG_PARAM(0xa));	
		}

		if( MyModule.sig == ON && sig == OFF ){	
			MyModule.sig = OFF;	
			LogIt(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_SIG_OFF,LOG_PARAM(0xb));	
		}

		
		//kang 2009-11-19--------------------------------------------------------------------------------------------------------------

		
		//---------------------------------
		//    STATUS�� ��� ����Ī ����.
		//---------------------------------
		SwitchingProcess(sig);

		//---------------------------------
		//    STATUS event �� ������ ����...
		//---------------------------------
		//if(TimeSliceCounter > 30){
			SwitchingEvent(sig);
		//}
		//---------------------------------
		//    LED - ACTIVE
		//---------------------------------

		//if(SYSTEM.OPERATION.OPMODE != OPMODE_POWERUP){
		//	if(IsSigOn()){		LED_ACT_ON();}
		//	else{				LED_ACT_OFF();}
		//}

		//---------------------------------
		//    Counter shift code I/O
		//---------------------------------
	}

	//----------------------------------------------------------
	//    Dual Status & Status/error code & counter ����ڵ�
	//----------------------------------------------------------
	SendMyStatus();
	
	if(PrintON==1){MyMonitorSTATUSout();}

}


void dual_a_send(void)
{
	DIAG.DUAL_TIMEOUT++;
	
	switch(DIAG.DUAL_STEP){
		case TRY_NULL:
				if((CounterModule.EXIST == YES) && (DIAG.EJT==OK) && (DIAG.DUAL_RX == OK) && (DIAG.DUAL_RX_CRC==OK) &&
					(DIAG.DUAL_TEST == UNDEFINED) && (dual_test_start==YES) && (dual_test_retry<10))
				{
					dual_test_retry++;
					//dual_test_start = NO;
					DIAG.DUAL_STEP = TRY_TESTMODE;
					DIAG.DUAL_TIMEOUT = 0;
					MessageOut("dual-channel test start\n");
					sprintf(MESG,"Dual channel test start, %d",dual_test_retry);
					LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, MESG);
				}
				//else{
				//	sprintf(MESG,"try null:%d,%d,%d,%d,%d,%d,%d\n",
				//		(CounterModule.EXIST == YES), (DIAG.EJT==OK), (DIAG.DUAL_RX == OK),
				//		(DIAG.DUAL_RX_CRC==OK),	(DIAG.DUAL_TEST == UNDEFINED), (dual_test_start==YES),
				//		(dual_test_retry<10));
				//	MessageOut(MESG);
				//}

				break;
				
		case TRY_TESTMODE:
				dual_data.reserved[1] = TRY_TESTMODE;
				MessageOut("send TRY_TESTMODE\n");
				break;
		
		case TRY_A_HIGH:
				dual_data.reserved[1] = TRY_A_HIGH;
				SetDualTxValueToHigh();
				MessageOut("send TRY_A_HIGH\n");
				break;
				
		case TRY_A_LOW:
				dual_data.reserved[1] = TRY_A_LOW; 
				SetDualTxValueToLow();
				MessageOut("send TRY_A_LOW\n");
				break;
				
		case TRY_B_HIGH:
				dual_data.reserved[1] = TRY_B_HIGH; 
				MessageOut("send TRY_B_HIGH\n");
				break;
				
		case TRY_B_LOW:
				dual_data.reserved[1] = TRY_B_LOW; 
				MessageOut("send TRY_B_LOW\n");
				break;
				
		case PROVE_B_HIGH_OK:
				dual_data.reserved[1] = PROVE_B_HIGH_OK; 
				DIAG.DUAL_STEP = TRY_B_LOW;
				MessageOut("send PROVE_B_HIGH_OK\n");
				break;
				
		case PROVE_B_HIGH_FAIL:
				dual_data.reserved[1] = PROVE_B_HIGH_FAIL; 
				SetDualTxModeToOperation();
				DIAG.DUAL_STEP = TRY_FINALIZE;
				MessageOut("send PROVE_B_HIGH_FAIL\n");
				break;
				
		case PROVE_B_LOW_OK:
				dual_data.reserved[1] = PROVE_B_LOW_OK; 
				SetDualTxModeToOperation();
				DIAG.DUAL_STEP = TRY_FINALIZE;
				MessageOut("send PROVE_B_LOW_OK\n");
				break;
				
		case PROVE_B_LOW_FAIL:
				dual_data.reserved[1] = PROVE_B_LOW_FAIL; 
				SetDualTxModeToOperation();
				DIAG.DUAL_STEP = TRY_FINALIZE;
				MessageOut("send PROVE_B_LOW_FAIL\n");
				break;
		
		case TRY_FINALIZE:
				//DIAG.DUAL_TIMEOUT = 0;
				break;
		
		default: 
				break;
	}
	
	if((DIAG.DUAL_STEP != TRY_NULL) && (DIAG.DUAL_STEP != TRY_FINALIZE)){
		dual_test_last_step = DIAG.DUAL_STEP;
	}
	
	if(DIAG.DUAL_TIMEOUT>10){
		DIAG.DUAL_TIMEOUT = 10;
		
		if(DIAG.DUAL_STEP != TRY_NULL){
			SetDualTxModeToOperation();
			//hmlee - aborted
			//Logitstring
			if(DIAG.DUAL_TEST == OK){LogItString(LOG_CATA1_NORM, LOG_CATA2_OK, "Dual channel test");}
			else if(DIAG.DUAL_TEST == FAIL){LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, "Dual channel test");}
			else{LogItString(LOG_CATA1_NORM, LOG_CATA2_WARN, "Dual channel test timeout");}
		}
		DIAG.DUAL_STEP = TRY_NULL;
	}
	
}


void dual_a_recv(int val)
{
int fpgaval;
	
	if(DIAG.DUAL_STEP == TRY_FINALIZE){
		MessageOut("finalizing\n");
		return;
	}
	
	switch(val){
		case PROVE_TESTMODE_OK:
				if(DIAG.DUAL_STEP == TRY_TESTMODE){
					SetDualTxModeToTest();	
					DIAG.DUAL_STEP = TRY_A_HIGH; 
					DIAG.DUAL_TIMEOUT = 0;
					dual_test_start = NO;
					if(DIAG.DUAL_TEST == OK){
						DIAG.DUAL_TEST = UNDEFINED;
					}
					MessageOut("recv PROVE_TESTMODE_OK\n");
				}
				else{
					MessageOut("recv PROVE_TESTMODE_OK, not in order\n");
				}
				
				break;
		
		case PROVE_TESTMODE_FAIL:
				if(DIAG.DUAL_STEP == TRY_TESTMODE){
					SetDualTxModeToOperation();
					DIAG.DUAL_STEP = TRY_FINALIZE; 
					DIAG.DUAL_TIMEOUT = 0;
					//DIAG.DUAL_TEST = FAIL;
					MessageOut("recv PROVE_TESTMODE_FAIL\n");
					LogItString(LOG_CATA1_NORM, LOG_CATA2_WARN, "Dual channel test rejected");
				}
				else{
					MessageOut("recv PROVE_TESTMODE_FAIL, not in order\n");
				}
				break;
				
		case PROVE_A_HIGH_OK:
				if(DIAG.DUAL_STEP == TRY_A_HIGH){
					DIAG.DUAL_STEP = TRY_A_LOW;
					DIAG.DUAL_TIMEOUT = 0; 
					MessageOut("recv PROVE_A_HIGH_OK\n");
				}
				else{
					MessageOut("recv PROVE_A_HIGH_OK, not in order\n");
				}
				break;
		
		case PROVE_A_HIGH_FAIL:
				if(DIAG.DUAL_STEP == TRY_A_HIGH){
					SetDualTxModeToOperation();
					DIAG.DUAL_STEP = TRY_FINALIZE;
					DIAG.DUAL_TIMEOUT = 0;
					DIAG.DUAL_TEST = FAIL;
					MessageOut("recv PROVE_A_HIGH_FAIL\n");
					LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, "Dual channel A-side high");
				}
				else{
					MessageOut("recv PROVE_A_HIGH_FAIL, not in order\n");
				}
				break;
				
		case PROVE_A_LOW_OK:
				if(DIAG.DUAL_STEP == TRY_A_LOW){
					DIAG.DUAL_STEP = TRY_B_HIGH; 
					DIAG.DUAL_TIMEOUT = 0;
					MessageOut("recv PROVE_A_LOW_OK\n");
				}
				else{
					MessageOut("recv PROVE_A_LOW_OK, not in order\n");
				}
				break;
		
		case PROVE_A_LOW_FAIL:
				if(DIAG.DUAL_STEP == TRY_A_LOW){
					SetDualTxModeToOperation();
					DIAG.DUAL_STEP = TRY_FINALIZE;
					DIAG.DUAL_TIMEOUT = 0;
					DIAG.DUAL_TEST = FAIL;
					MessageOut("recv PROVE_A_LOW_FAIL\n");
					LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, "Dual channel A-side low");
				}
				else{
					MessageOut("recv PROVE_A_LOW_FAIL, not in order\n");
				}
				break;
		
		case PROVE_B_HIGH_WAITING:	
				if(DIAG.DUAL_STEP == TRY_B_HIGH){
					MessageOut("recv PROVE_B_HIGH_WAITING\n");
					
					fpgaval = GetDualTxValueReceived();
					if((fpgaval&0x5B) == 0x5B){
						DIAG.DUAL_STEP = PROVE_B_HIGH_OK;
						DIAG.DUAL_TIMEOUT = 0;
						sprintf(MESG,"B_HIGH OK: 0x%x\n",fpgaval);
						MessageOut(MESG);
					}
					else{
						DIAG.DUAL_STEP = PROVE_B_HIGH_FAIL;
						DIAG.DUAL_TIMEOUT = 0;
						DIAG.DUAL_TEST = FAIL;
						sprintf(MESG,"B_HIGH FAIL: 0x%x\n",fpgaval);
						MessageOut(MESG);
						sprintf(MESG,"Dual channel B-side high, 0x%x",fpgaval);
						LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
					}
					dual_test_last_val = fpgaval;
				}
				else{
					MessageOut("recv PROVE_B_HIGH_WAITING, not in order\n");
				}
				break;
				
		case PROVE_B_LOW_WAITING:
				if(DIAG.DUAL_STEP == TRY_B_LOW){
					MessageOut("recv PROVE_B_LOW_WAITING\n");
				
					fpgaval = GetDualTxValueReceived();
					if((fpgaval&0x5B) == 0x00){
						DIAG.DUAL_STEP = PROVE_B_LOW_OK;
						DIAG.DUAL_TIMEOUT = 0;
						DIAG.DUAL_TEST = OK;
						sprintf(MESG,"B_LOW OK: 0x%x\n",fpgaval);
						MessageOut(MESG);
					}
					else{
						DIAG.DUAL_STEP = PROVE_B_LOW_FAIL;
						DIAG.DUAL_TIMEOUT = 0;
						DIAG.DUAL_TEST = FAIL;
						sprintf(MESG,"B_LOW FAIL: 0x%x\n",fpgaval);
						MessageOut(MESG);
						sprintf(MESG,"Dual channel B-side low, 0x%x",fpgaval);
						LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
					}
					dual_test_last_val = fpgaval;
if(dual_fail_debug){
	DIAG.DUAL_STEP = PROVE_B_LOW_FAIL;
	DIAG.DUAL_TIMEOUT = 0;
	DIAG.DUAL_TEST = FAIL;
	sprintf(MESG,"Dual channel B-side low, 0x%x",fpgaval);
	LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
}
				}
				else{
					MessageOut("recv PROVE_B_LOW_WAITING, not in order\n");
				}
				break;
				
		default:
				break;
	}
	
}


void dual_b_send(void)
{
	DIAG.DUAL_TIMEOUT++;
	
	switch(DIAG.DUAL_STEP){
		case PROVE_TESTMODE_OK:
				dual_data.reserved[1] = PROVE_TESTMODE_OK; 
				SetDualTxModeToTest();
				MessageOut("send PROVE_TESTMODE_OK\n");
				break;
		
		case PROVE_TESTMODE_FAIL:
				dual_data.reserved[1] = PROVE_TESTMODE_FAIL; 
				SetDualTxModeToOperation();
				DIAG.DUAL_STEP = TRY_FINALIZE;
				MessageOut("send PROVE_TESTMODE_FAIL\n");
				break;
				
		case PROVE_A_HIGH_OK:
				dual_data.reserved[1] = PROVE_A_HIGH_OK; 
				MessageOut("send PROVE_A_HIGH_OK\n");
				break;
				
		case PROVE_A_HIGH_FAIL:
				dual_data.reserved[1] = PROVE_A_HIGH_FAIL; 
				SetDualTxModeToOperation();
				DIAG.DUAL_STEP = TRY_FINALIZE;
				MessageOut("send PROVE_A_HIGH_FAIL\n");
				break;
		
		case PROVE_A_LOW_OK:
				dual_data.reserved[1] = PROVE_A_LOW_OK; 
				MessageOut("send PROVE_A_LOW_OK\n");
				break;
				
		case PROVE_A_LOW_FAIL:
				dual_data.reserved[1] = PROVE_A_LOW_FAIL; 
				SetDualTxModeToOperation();
				DIAG.DUAL_STEP = TRY_FINALIZE;
				MessageOut("send PROVE_A_LOW_FAIL\n");
				break;
		
		case PROVE_B_HIGH_WAITING:
				dual_data.reserved[1] = PROVE_B_HIGH_WAITING; 
				SetDualTxValueToHigh();
				MessageOut("send PROVE_B_HIGH_WAITING\n");
				break;
		
		case PROVE_B_LOW_WAITING:
				dual_data.reserved[1] = PROVE_B_LOW_WAITING;
				SetDualTxValueToLow();
				MessageOut("send PROVE_B_LOW_WAITING\n");
				break;
		
		case TRY_FINALIZE:
				//DIAG.DUAL_TIMEOUT = 0;
				break;
		
		default: 
				break;
	}
	
	if((DIAG.DUAL_STEP != TRY_NULL) && (DIAG.DUAL_STEP != TRY_FINALIZE)){
		dual_test_last_step = DIAG.DUAL_STEP;
	}
	
	if(DIAG.DUAL_TIMEOUT>10){
		DIAG.DUAL_TIMEOUT = 10;
		
		if(DIAG.DUAL_STEP != TRY_NULL){
			SetDualTxModeToOperation();
			//hmlee - aborted
			//Logitstring
			if(DIAG.DUAL_TEST == OK){LogItString(LOG_CATA1_NORM, LOG_CATA2_OK, "Dual channel test");}
			else if(DIAG.DUAL_TEST == FAIL){LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, "Dual channel test");}
			else{LogItString(LOG_CATA1_NORM, LOG_CATA2_WARN, "Dual channel test timeout");}
		}
		DIAG.DUAL_STEP = TRY_NULL;
	}
}


void dual_b_recv(int val)
{
int fpgaval;
	
	if(DIAG.DUAL_STEP == TRY_FINALIZE){
		MessageOut("finalizing\n");
		return;
	}
	
	switch(val){
		case TRY_TESTMODE:
				if(DIAG.DUAL_STEP == TRY_NULL){
					if((CounterModule.EXIST == YES) && (DIAG.DUAL_RX == OK) && (DIAG.DUAL_RX_CRC==OK)){
						DIAG.DUAL_STEP = PROVE_TESTMODE_OK;
						DIAG.DUAL_TIMEOUT = 0;
						if(DIAG.DUAL_TEST == OK){
							DIAG.DUAL_TEST = UNDEFINED;
						}
						MessageOut("recv TRY_TESTMODE, and ready\n");
						LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, "Dual channel test receive and ready");
					}
					else{
						DIAG.DUAL_STEP = PROVE_TESTMODE_FAIL;
						DIAG.DUAL_TIMEOUT = 0;
						MessageOut("recv TRY_TESTMODE, but not ready\n");
						//sprintf(MESG,"dch not ready:%d,%d,%d",
						//	(CounterModule.EXIST == YES),
						//	(DIAG.DUAL_RX == OK),
						//	(DIAG.DUAL_RX_CRC==OK)
						//);
						//LogItString(LOG_CATA1_NORM, LOG_CATA2_UNSORTED, MESG);
						LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, "Dual channel test receive but not ready");
					}
				}
				else{
					MessageOut("recv TRY_TESTMODE, not in order\n");
				}
				break;
				
		case TRY_A_HIGH:
				if(DIAG.DUAL_STEP == PROVE_TESTMODE_OK){
					MessageOut("recv TRY_A_HIGH\n");
				
					fpgaval = GetDualTxValueReceived();
					if((fpgaval&0x5B) == 0x5B){
						DIAG.DUAL_STEP = PROVE_A_HIGH_OK;
						DIAG.DUAL_TIMEOUT = 0;
						sprintf(MESG,"A_HIGH OK: 0x%x\n",fpgaval);
						MessageOut(MESG);
					}
					else{
						DIAG.DUAL_STEP = PROVE_A_HIGH_FAIL;
						DIAG.DUAL_TIMEOUT = 0;
						DIAG.DUAL_TEST = FAIL;
						sprintf(MESG,"A_HIGH FAIL: 0x%x\n",fpgaval);
						MessageOut(MESG);
						sprintf(MESG,"Dual channel A-side high, 0x%x",fpgaval);
						LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
					}
					dual_test_last_val = fpgaval;
				}
				else{
					MessageOut("recv TRY_A_HIGH, not in order\n");
				}
				break;
			
		case TRY_A_LOW:
				if(DIAG.DUAL_STEP == PROVE_A_HIGH_OK){
					MessageOut("recv TRY_A_LOW\n");
				
					fpgaval = GetDualTxValueReceived();
					if((fpgaval&0x5B) == 0x00){
						DIAG.DUAL_STEP = PROVE_A_LOW_OK;
						DIAG.DUAL_TIMEOUT = 0;
						sprintf(MESG,"A_LOW OK: 0x%x\n",fpgaval);
						MessageOut(MESG);
					}
					else{
						DIAG.DUAL_STEP = PROVE_A_LOW_FAIL;
						DIAG.DUAL_TIMEOUT = 0;
						DIAG.DUAL_TEST = FAIL;
						sprintf(MESG,"A_LOW FAIL: 0x%x\n",fpgaval);
						MessageOut(MESG);
						sprintf(MESG,"Dual channel A-side low, 0x%x",fpgaval);
						LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
					}
					dual_test_last_val = fpgaval;
if(dual_fail_debug){
	DIAG.DUAL_STEP = PROVE_A_LOW_FAIL;
	DIAG.DUAL_TIMEOUT = 0;
	DIAG.DUAL_TEST = FAIL;
	sprintf(MESG,"Dual channel A-side low, 0x%x",fpgaval);
	LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
}
				}
				else{
					MessageOut("recv TRY_A_LOW, not in order\n");
				}
				break;
				
		case TRY_B_HIGH:
				if(DIAG.DUAL_STEP == PROVE_A_LOW_OK){
					DIAG.DUAL_STEP = PROVE_B_HIGH_WAITING;
					DIAG.DUAL_TIMEOUT = 0;
					MessageOut("recv TRY_B_HIGH\n");
				}
				else{
					MessageOut("recv TRY_B_HIGH, not in order\n");
				}
				break;
		
		case TRY_B_LOW:
				if(DIAG.DUAL_STEP == PROVE_B_HIGH_OK){
					DIAG.DUAL_STEP = PROVE_B_LOW_WAITING;
					DIAG.DUAL_TIMEOUT = 0;
					MessageOut("recv TRY_B_LOW\n");
				}
				else{
					MessageOut("recv TRY_B_LOW, not in order\n");
				}
				break;
		
		case PROVE_B_HIGH_OK:
				if(DIAG.DUAL_STEP == PROVE_B_HIGH_WAITING){
					DIAG.DUAL_STEP = PROVE_B_HIGH_OK;
					DIAG.DUAL_TIMEOUT = 0;
					MessageOut("recv PROVE_B_HIGH_OK\n");
				}
				else{
					MessageOut("recv PROVE_B_HIGH_OK, not in order\n");
				}
				break;
				
		case PROVE_B_HIGH_FAIL:	
				if(DIAG.DUAL_STEP == PROVE_B_HIGH_WAITING){
					SetDualTxModeToOperation();
					DIAG.DUAL_STEP = TRY_FINALIZE;
					DIAG.DUAL_TIMEOUT = 0;
					DIAG.DUAL_TEST = FAIL;
					MessageOut("recv PROVE_B_HIGH_FAIL\n");
					LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, "Dual channel B-side high");
				}
				else{
					MessageOut("recv PROVE_B_HIGH_FAIL, not in order\n");
				}
				break;
		
		case PROVE_B_LOW_OK:
				if(DIAG.DUAL_STEP == PROVE_B_LOW_WAITING){
					SetDualTxModeToOperation();
					DIAG.DUAL_STEP = TRY_FINALIZE;
					DIAG.DUAL_TIMEOUT = 0;
					DIAG.DUAL_TEST = OK;
					MessageOut("recv PROVE_B_LOW_OK\n");
				}
				else{
					MessageOut("recv PROVE_B_LOW_OK, not in order\n");
				}
				break;
				
		case PROVE_B_LOW_FAIL:
				if(DIAG.DUAL_STEP == PROVE_B_LOW_WAITING){
					SetDualTxModeToOperation();
					DIAG.DUAL_STEP = TRY_FINALIZE;
					DIAG.DUAL_TIMEOUT = 0;
					DIAG.DUAL_TEST = FAIL;
					MessageOut("recv PROVE_B_LOW_FAIL\n");
					LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, "Dual channel B-side low");
				}
				else{
					MessageOut("recv PROVE_B_LOW_FAIL, not in order\n");
				}
				break;
	}
		
}




#define bit8Display(s1,s2,s3,s4,s5,s6,s7,s8,v) printf(s1":%1d "s2":%1d "s3":%1d "s4":%1d "s5":%1d "s6":%1d "s7":%1d "s8":%1d " \
			,(((v)&wbD0)==wbD0),(((v)&wbD1)==wbD1),(((v)&wbD2)==wbD2),(((v)&wbD3)==wbD3),(((v)&wbD4)==wbD4),(((v)&wbD5)==wbD5),(((v)&wbD6)==wbD6),(((v)&wbD7)==wbD7) );


int PrintON =0;
void MyMonitorSTATUSForDebug()
{
#define MMS_NUM 500 // MMS_NUM: MyMonitorStatus

	switch(iLsiDebugVariable)
	{
		case MMS_NUM:
				//ConditionalSigOn();
				SigOnWithReadyOff();	
				printf("\r\n"
				"ConditionalSigOn**********\n");
			break;

		case MMS_NUM+1:	
				//ConditionalSigOff();
				SigOffWithReadyOn();	
				printf("\r\n"
				"ConditionalSigOff**********\n");
			break;

		case MMS_NUM+2:	
				UnconditionalSigOn();	
				printf("\r\n"
				"UnConditionalSigOn**********\n");
			break;

		case MMS_NUM+3:	
				UnconditionalSigOff();	
				printf("\r\n"
				"UnConditionalSigOff**********\n");
			break;

		case MMS_NUM+4:	
				SetMeFail();	
				printf("\r\n"
				"SetMeFail**********\n");
			break;
			
		case MMS_NUM+5:	
				ClearMyFail();	
				printf("\r\n"
				"ClearMyFail**********\n");
			break;
			
		
	}
	iLsiDebugVariable =0;
	
	
	if(PrintON==1){MyMonitorSTATUSout();}
	

}


void MyMonitorSTATUSout()
{
	int v;
	
	printf("%c[1;1H=>sig:%d *",0x1B, (int)IsSigOn() );

	v=GetFpgaValue(25);
	//printf("\n25: 0x%04X ",v);
	printf("\n");
	//printf("%c[2;1H",0x1B);
	bit8Display("25* ene","ibe","ube","cle","* enm","---","---","---", v )
	
	v=GetFpgaValue(26);
	//printf("\n26: 0x%04X ",v);
	printf("\n");
	//printf("%c[3;1H",0x1B);
	bit8Display("26* mop","sid","ufl","usb","* uop","usl","url","sig", v )
	
	v=GetFpgaValue(38);
	//printf("\n38: 0x%04X ",v);
	printf("\n");
	//printf("%c[4;1H",0x1B);
	bit8Display("38* fsu","rst","ufl","osc","* hwe","isl","ifl","rsh", v )
	
	v=GetFpgaValue(39);
	//printf("\n39: 0x%04X ",v);
	printf("\n");
	//printf("%c[5;1H",0x1B);
	bit8Display("39* usl","uop","ufl","url","* irl","---","---","---", v )
	
	printf("\n");
	//printf("%c[6;1H",0x1B);
	printf("[Pri]MM:%d/CM:%d",MyModule.priority, CounterModule.priority);
	
	printf("\n");
	//printf("%c[7;1H",0x1B);
	printf("[Ref]MM:%d/CM:%d",MyModule.reference, CounterModule.reference);
	
	printf("\n");
	//printf("%c[8;1H",0x1B);
	printf("[Mod]MM:%d/CM:%d",MyModule.LockMode, CounterModule.LockMode);

	printf("\n");
	//printf("%c[9;1H",0x1B);
	printf("OPmode:0x%02X    FIRST_LOCK:%d", TOD.opmode, FIRST_LOCK);
	
	printf("\n");
	//printf("%c[10;1H",0x1B);
	printf("SideOCXO:%5.2f    Board:%5.2f", (float)ts.ocxo_temp, (float)ts.board_temp);

	printf("\n");
	//printf("%c[11;1H",0x1B);
	printf("[New Pri]MM:%d/CM:%d", psda.my_priority_value, psda.your_priority_value);
	
	printf("\n");

}


void DisplayMessage(void)
{
	if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUGD){

	   	sprintf((char*)MESG,"%3d:%2d:%2d:%2d %3u> S:%d/%d %d R:%7.2f M:%7.2f MO:%5.2f RD:%5.2f TD:%5.2f %.3d P:%.2fV +5V:%.2f VCC:%.2f VEE:%.2f\r\n",
              	CONTROL.StepCounter,SYSTEM.TIME.CUR.HOUR,SYSTEM.TIME.CUR.MINUTE,SYSTEM.TIME.CUR.SECOND,
               	CONTROL.IntervalCounter,
               	SYSTEM.OPERATION.STATUS,CounterModule.STATUS,CounterModule.EXIST,
               	CONTROL.RealDAC,MeanDAC,Modified_RAW_OFFSET,RAW_DIFFERENCE,OCXO_ID.TDAC,usADCValue,fVCXOEfc,fPlus5V,fPlusVcc,fMinusVee);

		TransmitMessage((BYTE*)MESG,DEBUG_PORT);
	}
}


int GetParameter1()
{
	int Parameter1;
	unsigned short MyDualSigInfo,YourDualSigInfo;
	unsigned short FSA,DA;

	Parameter1 = 0;

	MyDualSigInfo = YourDualSigInfo = 0;

	FSA = 0;
	DA = 0;//GetFpgaValue(DebugAddr);

	//MyDualSigInfo = (FSA&0x000F);
	MyDualSigInfo = (FSA&0x00F0);

	//YourDualSigInfo = (DA&0x000F);
	YourDualSigInfo = (DA&0x00F0);
	/*
	if( DIAG.u_exist == NO ){
		YourDualSigInfo = (0x01<<3)|YourDualSigInfo;
	}
	*/

	Parameter1 = (int)((MyDualSigInfo<<8)|YourDualSigInfo);

	return Parameter1;
}

int GetParameter2()
{
	int Parameter2;
	Parameter2 = 0;
	Parameter2 = (TOD.opmode<<8)|(TOD.inform);

	return Parameter2;
}

int GetParameter3()
{
	if(	Status_DualRx == NO){
		return 1;
	}
	else {
		return 0;
	}
}


int SigPrev=0;
int CntSatusPrev=0;
int SatusPrev=0;
int YourSblockPrev=0;
int YourBlockPrev=0;
int SblockPrev=0;
int BlockPrev=0;


#define MAX_STATUS_ARRAY 8
char* status_str_array[MAX_STATUS_ARRAY] = {
	"NULL",
	"ACTIVE", 			"STANDBY", 				"BLOCK",
	"PRE_STANDBY", 		"ACTIVE_BLOCKED", 		"UNKNOWN",
	"NOT_EXIST"
};
	
void LogDualDebug(int SIG)
{
char buf[32];
	//int bChanged=0;


	CheckPriority();
	if( SigPrev !=SIG )
	{
		if(SIG) {
			//sprintf(buf,"..on..(0x%x)",GetDualEvntLatch());
			//LogItString(buf);
			LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, "..on..");
		}
		else{
			//sprintf(buf,"..off..(0x%x)",GetDualEvntLatch());
			//LogItString(buf);
			LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, "..off..");
		}
		
		//ClearEvntLatch();
	}
	SigPrev  = SIG;

	return ;

	
	if( (CntSatusPrev != CounterModule.STATUS) && (DIAG.DUAL_STEP == TRY_NULL)){
		//LogItString2("Counter Status Changed...>>",CntSatusPrev, CounterModule.STATUS );
		sprintf(MESG,"U-self, %s to %s",
			((CntSatusPrev>=NOT_EXIST) ? status_str_array[MAX_STATUS_ARRAY-1] : status_str_array[CntSatusPrev]),
			((CounterModule.STATUS>=NOT_EXIST) ? 
				status_str_array[MAX_STATUS_ARRAY-1] : 
				status_str_array[CounterModule.STATUS]
			)
		);
		LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, MESG);
	}
	CntSatusPrev  =  CounterModule.STATUS ;
	
	if( SatusPrev != SYSTEM.OPERATION.STATUS ){
		//LogItString2("My Status Changed...>>",SatusPrev, SYSTEM.OPERATION.STATUS );
		sprintf(MESG,"M-self, %s to %s",
			((SatusPrev>=NOT_EXIST) ? status_str_array[MAX_STATUS_ARRAY-1] : status_str_array[SatusPrev]),
			((SYSTEM.OPERATION.STATUS>=NOT_EXIST) ? 
				status_str_array[MAX_STATUS_ARRAY-1] : 
				status_str_array[SYSTEM.OPERATION.STATUS]
			)
		);
		LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, MESG);
	}
	SatusPrev  =  SYSTEM.OPERATION.STATUS ;

	if( YourBlockPrev != QueryYourForceBlock() || YourSblockPrev != QueryYourSelfBlock() ){
		LogItString2(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
			"------ Your Cmd Changed..............##",QueryYourForceBlock(), QueryYourSelfBlock() );
	}
	YourBlockPrev  =  QueryYourForceBlock();
	YourSblockPrev =  QueryYourSelfBlock();


	if( BlockPrev != QueryForceBlock() || SblockPrev != QuerySelfBlock() ){
		LogItString2(LOG_CATA1_NORM, LOG_CATA2_NOTHING, 
			"------ My  Cmd Changed..............$$",QueryForceBlock(), QuerySelfBlock() );
	}
	BlockPrev  =  QueryForceBlock();
	SblockPrev =  QuerySelfBlock();
	
	
}


int val11_prev, val21_prev, val22_prev, val27_prev;
void LogDualExt(void)
{
int val11_cur, val21_cur, val22_cur, val27_cur;

	val11_cur = (GetFpgaValue(11))&0x5E;
	val21_cur = GetFpgaValue(21);
	val22_cur = GetFpgaValue(22);
	val27_cur = GetFpgaValue(27);
	
	return ;

	if((val11_cur != val11_prev) && (DIAG.DUAL_STEP==TRY_NULL)){
		sprintf(MESG,"Fpga 11, 0x%x->0x%x", val11_prev, val11_cur);
		LogItString(LOG_CATA1_SPEC, LOG_CATA2_NOTHING, MESG);
	}
	
	if(((val21_cur != val21_prev) || (val22_cur != val22_prev)) &&
		(DIAG.DUAL_STEP==TRY_NULL))
	{
		sprintf(MESG,"Fpga 21/22, 0x%x->0x%x, 0x%x->0x%x", 
			val21_prev, val21_cur, val22_prev, val22_cur
		);
		LogItString(LOG_CATA1_SPEC, LOG_CATA2_NOTHING, MESG);
		
		ClearEvntLatch();
	}
	
	
	if((val27_cur != val27_prev) && (DIAG.DUAL_STEP==TRY_NULL)){
		sprintf(MESG,"Fpga 27, 0x%x->0x%x", val27_prev, val27_cur);
		LogItString(LOG_CATA1_SPEC, LOG_CATA2_NOTHING, MESG);
	}
	
	val11_prev = val11_cur;
	val21_prev = val21_cur;
	val22_prev = val22_cur;
	val27_prev = val27_cur;
}


